#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_Types.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_Lib/NetEngine_BaseLib/BaseLib_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_Lib/NetEngine_BaseLib/BaseLib_Error.h"

//g++ -std=gnu++11 -Wall -g Test_BaseLib.cpp -o Test_BaseLib -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib -lNetEngine_BaseLib -Wl,-rpath=../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib -lpthread
XNETEVENT hEvent;
LPVOID Thread_TestEvent(LPVOID lParam)
{
    printf("Thread_TestEvent Start\n");
    int i = 0;
    while (1)
    {
        if (BaseLib_OperatorEvent_Wait(hEvent))
        {
            printf("1 %d\n",i++);
        }
        else
        {
            printf("2 %d\n",i++);
            break;
        }
        sleep(1);
    }
    printf("Thread_TestEvent End\n");

    return NULL;
}
void EventTest()
{
    hEvent = BaseLib_OperatorEvent_Create(TRUE);
    if (NULL == hEvent)
    {
        return;
    }
    pthread_t hThread;
    pthread_create(&hThread,NULL,Thread_TestEvent,NULL);

    printf("BaseLib_OperatorEvent_Avtive 1\n");
    BaseLib_OperatorEvent_Avtive(hEvent);
    sleep(5);
    printf("BaseLib_OperatorEvent_Avtive 2\n");
    BaseLib_OperatorEvent_Reset(hEvent);
    sleep(5);
    printf("BaseLib_OperatorEvent_Avtive 3\n");
    BaseLib_OperatorEvent_Avtive(hEvent);
    sleep(5);
    printf("BaseLib_OperatorEvent_Avtive 4\n");
    BaseLib_OperatorEvent_Reset(hEvent);

    printf("EventTest:ok\n");
    BaseLib_OperatorEvent_Delete(hEvent);
}
void TimeSpanTest()
{
    __int64 nDayTTime = 0;
    __int64 nHourTTime = 0;
    LPCTSTR lpszStartTime = _T("2019-11-01 12:31:10");
    LPCTSTR lpszEndTime = _T("2020-10-02 12:31:10");
    NETENGINE_LIBTIMER st_LibTimer;
    memset(&st_LibTimer, '\0', sizeof(NETENGINE_LIBTIMER));

    BaseLib_OperatorTimeSpan_GetForStr(lpszStartTime, lpszEndTime, &nDayTTime, 0);
    BaseLib_OperatorTimeSpan_GetForStr(lpszStartTime, lpszEndTime, &nHourTTime, 1);
    _tprintf(_T("\n跨了：%ld天 %ld小时\n"), nDayTTime, nHourTTime);

    BaseLib_OperatorTimeSpan_CalForStr(lpszStartTime, lpszEndTime, &st_LibTimer, TRUE);
    _tprintf(_T("%d/%d/%d-%d:%d:%d\n"), st_LibTimer.wYear, st_LibTimer.wMonth, st_LibTimer.wDay, st_LibTimer.wHour, st_LibTimer.wMinute, st_LibTimer.wSecond);

    BaseLib_OperatorTimeSpan_CalForStr(lpszStartTime, lpszEndTime, &st_LibTimer, FALSE);
    _tprintf(_T("%d/%d/%d-%d:%d:%d\n"), st_LibTimer.wYear, st_LibTimer.wMonth, st_LibTimer.wDay, st_LibTimer.wHour, st_LibTimer.wMinute, st_LibTimer.wSecond);
}
void TimeTest()
{
    NETENGINE_LIBTIMER st_LibTimer;
    memset(&st_LibTimer, '\0', sizeof(NETENGINE_LIBTIMER));

    BaseLib_OperatorTime_GetSysTime(&st_LibTimer);
    BaseLib_OperatorTime_TTimeToStuTime(time(NULL), &st_LibTimer);

    XNETHANDLE xhXTPTime = 0;
    memset(&st_LibTimer, '\0', sizeof(NETENGINE_LIBTIMER));

    BaseLib_OperatorTime_SetXTPTime(&xhXTPTime);
    BaseLib_OperatorTime_GetXTPTime(xhXTPTime,&st_LibTimer);
}
void StringTest()
{
    LPCTSTR lpszFile = _T("./adadad/file.txt");
    TCHAR tszFileDir[MAX_PATH];
    TCHAR tszFileName[MAX_PATH];

    memset(tszFileDir, '\0', MAX_PATH);
    memset(tszFileName, '\0', MAX_PATH);
    BaseLib_OperatorString_GetFileAndPath(lpszFile, tszFileDir, tszFileName);
}


void HandleTest()
{
    TCHAR tszMsgBuffer[64];
    memset(tszMsgBuffer, '\0', 64);
    BaseLib_OperatorHandle_CreateGuid(tszMsgBuffer, TRUE, FALSE);
    printf("%s\n", tszMsgBuffer);

    typedef struct
    {
        int a;
        int b;
    }NETENGINE_HANDLETEST;

    XNETHANDLE xhNet = 0;
    NETENGINE_HANDLETEST st_HandleTest;
    st_HandleTest.a = 1;
    st_HandleTest.b = 2;

    BaseLib_OperatorHandle_Add(&xhNet,sizeof(NETENGINE_HANDLETEST));
    memset(&st_HandleTest, '\0', sizeof(NETENGINE_HANDLETEST));
    BaseLib_OperatorHandle_Get(xhNet, &st_HandleTest);
    st_HandleTest.a = 3;
    st_HandleTest.b = 4;
    BaseLib_OperatorHandle_Set(xhNet, &st_HandleTest);
    memset(&st_HandleTest, '\0', sizeof(NETENGINE_HANDLETEST));
    BaseLib_OperatorHandle_Get(xhNet, &st_HandleTest);
    BaseLib_OperatorHandle_Del(xhNet);
}

void GMTTimeTest()
{
    TCHAR tszGMTTime[128];
    memset(tszGMTTime, '\0', sizeof(tszGMTTime));
    BaseLib_OperatorTime_GMTTime(tszGMTTime);

    printf("%s\n", tszGMTTime);
}
void FromCharGetKeyValue()
{
    LPCTSTR lpszStrSource = _T("123: 321");
    TCHAR tszKey[32];
    TCHAR tszValue[32];

    memset(tszKey, '\0', sizeof(tszKey));
    memset(tszValue, '\0', sizeof(tszValue));

    BaseLib_OperatorString_FromCharGetKeyValue(lpszStrSource, ':', tszKey, tszValue);
    printf("%s = %s\n", tszKey, tszValue);


    LPCTSTR lpszStrDest = _T("asd=zxc");
    memset(tszKey, '\0', sizeof(tszKey));
    memset(tszValue, '\0', sizeof(tszValue));

    BaseLib_OperatorString_FromCharGetKeyValue(lpszStrDest, '=', tszKey, tszValue);
    printf("%s = %s\n", tszKey, tszValue);
}

int TestAddrLib()
{
    LPCTSTR lpszIPV4Convert = _T("192.0.*.10");
    LPCTSTR lpszIPV6Convert = _T("ACC::1101");

    NETENGINE_LIBADDR st_LibAddr;
    NETENGINE_LIBADDR st_LibAddrV6;
    memset(&st_LibAddr, '\0', sizeof(NETENGINE_LIBADDR));
    memset(&st_LibAddrV6, '\0', sizeof(NETENGINE_LIBADDR));

    BaseLib_OperatorString_AddrStruct(lpszIPV4Convert, &st_LibAddr,TRUE);
    BaseLib_OperatorString_AddrStruct(lpszIPV6Convert, &st_LibAddrV6,TRUE);
    return 1;
}

int main()
{
    EventTest();
    while (1)
    {
        sleep(1);
    }
    TimeTest();
    TimeSpanTest();
    GMTTimeTest();
    FromCharGetKeyValue();
    TestAddrLib();
    return 0;
}
